//
//  HNSH_CashierPayHeader.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/2.
//

#import "HNSH_CashierPayHeader.h"

//V
#import "MZTimerLabel.h"

@interface HNSH_CashierPayHeader ()

/**  */
@property (nonatomic, strong)UILabel * titleLabel;
/**  */
@property (nonatomic, strong)UILabel * priceLabel;
/**  */
@property (nonatomic, strong)UILabel * timeLabel;
/**  */
@property (nonatomic, strong)UILabel * timeBgLabel;
/**  */
@property (nonatomic, strong)MZTimerLabel * time;

@end

@implementation HNSH_CashierPayHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor appBGColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Cycle Methods
- (void)addAllViews{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.titleLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.timeLabel];
    [self addSubview:self.timeBgLabel];
    [self addSubview:self.time];
}

- (void)addAllLayout{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(29);
        make.height.mas_equalTo(20);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(6);
        make.height.mas_equalTo(55);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_centerX).offset(10);
        make.top.mas_equalTo(self.priceLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
    }];
    [self.timeBgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_centerX).offset(10);
        make.top.mas_equalTo(self.priceLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
    }];
}

//MARK: Setter Methods
- (void)setModel:(HNSH_CashierPayModel *)model{
    _model = model;
    
    self.titleLabel.text = model.title ? : @"支付金额";
    NSString * p = [NSString stringWithFormat:@"%@%.2f", model.amount_symbol, [model.amount doubleValue]];
    if (model.sub_amount_text.length > 0) {
        p = [NSString stringWithFormat:@"%@+%@", p, model.sub_amount_text];
        self.priceLabel.font = [UIFont boldSystemFontOfSize:20];
        self.priceLabel.text = p;
    } else {
        NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString:p];
        if (model.amount_symbol.length) {
            [attStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:24]} range:[p rangeOfString:model.amount_symbol]];
        }
        self.priceLabel.attributedText = attStr;
    }
    self.timeLabel.text = model.expired_desc ? : @"支付剩余时间：";
    [self.time setCountDownTime:[model.expired_seconds longLongValue]];
    [self.time start];
}

//MARK: Getter Methods
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor appGrayLabelColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.textColor = [UIColor appBlackLabelColor];
        _priceLabel.font = [UIFont boldSystemFontOfSize:38];
        _priceLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = [UIColor appGrayLabelColor];
        _timeLabel.font = [UIFont systemFontOfSize:14];
    }
    return _timeLabel;
}

- (UILabel *)timeBgLabel{
    if (!_timeBgLabel) {
        _timeBgLabel = [UILabel new];
        _timeBgLabel.textColor = [UIColor appGrayLabelColor];
        _timeBgLabel.font = [UIFont systemFontOfSize:14];
        _timeBgLabel.text = @"00:00:00";
    }
    return _timeBgLabel;
}

- (MZTimerLabel *)time{
    if (!_time) {
        _time = [[MZTimerLabel alloc] initWithLabel:self.timeBgLabel andTimerType:MZTimerLabelTypeTimer];
        _time.timeFormat = @"HH:mm:ss";
    }
    return _time;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
